﻿using AutoMapper;
using Device.CommonLib.Models;
using Device.DomainLib.Aggregates;
using Device.DomainLib.Repositories;
using Swimj.CommonLib.Base;
using Swimj.Data.Abstractions;

namespace Device.ApplicationLib.Queries
{
    public class CategoryQuery : ICategoryQuery
    {
        private readonly ICategoryRepository _categoryRepository;
        private readonly IMapper _mapper;

        public CategoryQuery(ICategoryRepository categoryRepository,
            IMapper mapper)
        {
            _categoryRepository = categoryRepository;
            _mapper = mapper;
        }

        public async Task<PaginatedResult<CategoryInfoResponseDto>> QueryCategoriesByPageAsync(
            CategorySearchRequestDto? model)
        {
            var conditionCollection = new ConditionCollection();
            if (model != null)
            {
                var result =
                    await _categoryRepository.QueryFilterByOrderPageAsync(conditionCollection,
                        nameof(CategoryAggregate.Sort), "asc", model.Page, model.Limit);

                return await PaginatedResult<CategoryInfoResponseDto>.SuccessAsync(
                    _mapper.Map<List<CategoryInfoResponseDto>>(result.Item1),
                    result.Item2,
                    model.Page, model.Limit);
            }

            return await PaginatedResult<CategoryInfoResponseDto>.SuccessAsync(
                new List<CategoryInfoResponseDto>(), 0, 0, 0);
        }

        public Task<Result<List<CategoryInfoResponseDto>>> QueryCategoriesAsync(CategorySearchRequestDto? model)
        {
            throw new NotImplementedException();
        }
    }
}